// 初始化一些全局功能
$(function () {
    function handler() {
        // 禁止所有a标签和img标签拖动
        $('a, img').attr('draggable', 'false');
    }
    handler();
    // Pjax 重载函数
    document.addEventListener('pjax:complete', handler);
});

// 初始化 Pjax
$(function () {
    new Pjax({
        elements: [
            'a[href]:not([href^="#"]):not([href="javascript:void(0)"])',
            'form',
        ],
        selectors: [
            "#main",
        ],
        cacheBust: false,
    });

    document.addEventListener('pjax:send', function () {
        NProgress.start();
    });

    document.addEventListener('pjax:complete', function () {
        NProgress.done();
        // 重载js文件
        $('script[data-pjax], .pjax-reload script').each(function () {
            $(this).parent().append($(this).remove());
        });
    });

    document.addEventListener('pjax:error', function () {
        NProgress.done();
    });
});

// 初始化 APlayer
$(function () {
    var ap = new APlayer({
        container: document.getElementById('aplayer'),
        fixed: true,
        listFolded: true,
        audio: [],
        theme: '#292D33',
    });

    function createPlayBtn() {
        var btn = document.createElement('div');
        btn.innerHTML = '播放'
        return btn;
    }

    function handler() {
        var audios = document.querySelectorAll('.mp3-item');

        if (audios.length > 0) {
            var playBtn = createPlayBtn();

            audios[0].parentNode.insertBefore(playBtn, audios[0]);

            var playList = [];

            for (var i = 0; i < audios.length; i++) {
                playList.push(audios[i].dataset);
            }

            playBtn.addEventListener('click', function () {
                // 清空旧播放列表
                if (ap.list.audios.length > 0) {
                    // 修复播放器清空列表时的错误
                    if (!ap.list.lrc) {
                        ap.list.lrc = {};
                    }
                    ap.list.clear();
                }
                // 替换新的播放列表
                ap.list.add(playList);
                ap.play();
                // 展开播放器
                document.querySelector('.aplayer').classList.remove('aplayer-narrow');
            })
        }
    }
    // 页面刷新后执行
    handler();
    // Pjax 重载函数
    document.addEventListener('pjax:complete', handler);
});

// 绘制背景
$(function () {
    document.addEventListener('touchmove', function (e) {
        e.preventDefault();
    })
    var canvas = document.createElement('canvas');
    canvas.className = "nole";
    var x = canvas.getContext('2d'),
        pr = window.devicePixelRatio || 1,
        w = window.innerWidth * 1.5,
        h = window.innerHeight * 1.5,
        f = 90,
        q,
        m = Math,
        r = 0,
        u = m.PI * 2,
        v = m.cos,
        z = m.random
    canvas.width = w * pr
    canvas.height = h * pr
    x.scale(pr, pr)
    x.globalAlpha = 0.6
    // x.globalAlpha = 1
    function i() {
        x.clearRect(0, 0, w, h)
        q = [{ x: 0, y: h * .7 + f }, { x: 0, y: h * .7 - f }]
        while (q[1].x < w + f) d(q[0], q[1])
    }
    function d(i, j) {
        x.beginPath()
        x.moveTo(i.x, i.y)
        x.lineTo(j.x, j.y)
        var k = j.x + (z() * 2 - 0.25) * f,
            n = y(j.y)
        x.lineTo(k, n)
        x.closePath()
        r -= u / -50
        // x.fillStyle = '#'+(v(r)*127+128<<16 | v(r+u/3)*127+128<<8 | v(r+u/3*2)*127+128).toString(16)
        // x.strokeStyle = '#'+(v(r)*127+128<<16 | v(r+u/3)*127+128<<8 | v(r+u/3*2)*127+128).toString(16)
        // x.lineWidth = 2;
        // x.strokeStyle = '#ffffff';
        x.fillStyle = '#292D33';

        // x.stroke();
        x.fill();
        q[0] = q[1]
        q[1] = { x: k, y: n }
    }
    function y(p) {
        var t = p + (z() * 2 - 1.1) * f
        return (t > h || t < 0) ? y(p) : t
    }
    document.onclick = i
    // document.ontouchstart = i
    i();

    canvas.style = 'position:fixed;top:0;left:0;width:100%;height:100%;z-index:0;opacity:1';
    document.body.append(canvas);
});

// 文章标题跳转器
$(function () {
    // function handler() {
    //     var h2s = document.querySelectorAll('.post-content h2');
    //     if (h2s.length > 0) {
    //         var jumper = document.querySelector('#jumper');
    //         var body = document.querySelector('#jumper .body');
    //         var template = '<ul class="list">';
    //         for (var i = 0; i < h2s.length; i++) {
    //             var h2 = h2s[i];
    //             var mao = Date.now() * Math.random();
    //             h2.style = "padding-top: 100px;margin-top: -100px;"
    //             h2.id = mao;
    //             template += '<li><a href="#' + mao + '">' + h2.innerHTML + '</a></li>'
    //         }
    //         template += '</ul>';
    //         body.innerHTML = template;
    //         jumper.className = "kit-hidden-tb active";
    //     }
    // }
    // handler();
    // // Pjax 重载函数
    // document.addEventListener('pjax:complete', handler);
});

// 分类菜单折叠
$(function () {
    var parents = $('#aside .widget-list .category-parent');
    var counts = $('#aside .widget-list .category-parent .count');

    function onlyHandler() {
        $('.category-level-0.category-parent.active').removeClass('active').children('.count').html('展开');
    }

    parents.each(function () {
        var parent = $(this);
        var countBtn = parent.children('.count');
        counts.show();
        if (parent.children('.widget-list').length > 0) {
            var aBtn = parent.children('a');
            countBtn.html('展开');
            countBtn.addClass('count-btn');
            countBtn.on('click', function () {
                if (parent.hasClass('active')) {
                    parent.removeClass('active');
                    countBtn.html('展开');
                } else {
                    onlyHandler();
                    parent.addClass('active');
                    countBtn.html('收起');
                }
            });
            aBtn.on('click', function () {
                if (!parent.hasClass('active')) {
                    onlyHandler();
                    parent.addClass('active');
                    countBtn.html('收起');
                }
            });

        }
    })
});

// 打字效果
$(function () {
    // new Typed('#logo', {
    //     strings: ["MIMIVA^1000"], //输入内容, 支持html标签
    //     typeSpeed: 100, //打字速度
    //     backSpeed: 50, //回退速度
    //     loop: true,
    //     loopCount: Infinity
    // });

});